package com.libg.cn.sort;

import lombok.Builder;

/*
选择排序
 */
@Builder
public class SelectionSort {

    public static <E extends Comparable<E>> void sort(E[] arrs) {

        for (int i = 0; i < arrs.length; ++i) {
            int minIndex = i;
            for (int j = i; j < arrs.length; ++j) {
                if (arrs[j].compareTo(arrs[minIndex]) < 0) {
                    minIndex = j;
                }
                swap(arrs, i, minIndex);
            }
        }
    }

    private static <E> void swap(E[] arrs, int i, int j) {
        E temp = arrs[i];
        arrs[i] = arrs[j];
        arrs[j] = temp;
    }
}
